<template>
    <view class="container">
        <view class="practise_error_top" :style="'background:linear-gradient(180deg,'+(defaultColor||'#fff')+' 0%,'+(secondColor||'#fff')+' 100%)'">
            <view class="practise_all_block">
                <view class="practise_all_error" :style="'color:'+defaultColor">
                    <text class="practise_all_error_num">{{listPage.totalSize}}</text>
                    <text class="practise_all_error_text">我的错题</text>
                </view>
            </view>
            <view class="tab_list" :style="'color:'+defaultColor">
                <view class="tab_item">今日错题</view>
                <view class="tab_item">全部错题</view>
            </view>
        </view>
        <view class="practise_error_list">
            <view class="practise_error_item" v-for="(item,index) in practiseErrorList" :data-url="'practise_detail.html?recordId='+item.practiseRecordId+'&questionId='+item.questionId"  @click="tolinkUrl">
                <view class="practise_error_item_left">
                    <text class="practise_error_item_title">{{item.questionTitle}}</text>
                </view>
                <view class="practise_error_item_right">
                    <text class="practise_error_num" :style="'color:'+defaultColor">{{item.questionNum||1}}</text>
                    <image class="practise_error_link_icon" src="https://image1.sansancloud.com/xianhua/2020_1/2/18/38/31_120.jpg?x-oss-process=style/preview_120"></image>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import { socketFun } from "../../wxcomponents/public/json2Form";
export default {
  data() {
    return {
      practiseErrorList:null,
      listPage:{
          page: 1,
          pageSize: 20,
          totalSize: 0,
      },
      secondColor:'',
      defaultColor:'',
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    let that=this;
    console.warn("======onLoad:options======", options, getApp().globalData.setting);
    that.getPractiseErrorData(that.listPage);
  },
  onReady: function () {
    
  },
  onShow: function () {
    let that=this;
	that.setData({
        setting:getApp().globalData.setting,
        defaultColor:getApp().globalData.setting.platformSetting.defaultColor,
        secondColor:getApp().globalData.setting.platformSetting.secondColor,
       })
    wx.setNavigationBarColor({
      frontColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topColor.toLowerCase():'',
      backgroundColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topBgColor:''
    });
  },
  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; // 下拉刷新的时候首先判断存不存在tab
    console.log("下拉");
    that.listPage.page=1
    that.getPractiseErrorData(that.listPage);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('===onReachBottom====');
    let that = this
    if (that.listPage.totalSize > that.listPage.page * that.listPage.pageSize) {
      that.listPage.page++
      that.getPractiseErrorData(that.listPage,'upload');
    }else{
        wx.showToast({
          title: "到底了~",
          image: '/wxcomponents/images/icons/tip.png',
          duration: 1000
        });
    }
  },
  methods: {
    /* 获取数据 */
    getPractiseErrorData: function (param) {
      var that = this
      let params=Object.assign({},param,{
      })
      console.log("===params==",params)
      var customIndex = getApp().globalData.AddClientUrl("/wx_find_user_course_practise_error_items.html", params)
      getApp().globalData.showToastLoading('loading', true)
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          wx.hideLoading()
          console.log(res.data)
          let data=res.data.relateObj.result
          that.listPage.pageSize = res.data.relateObj.pageSize
          that.listPage.totalSize = res.data.relateObj.totalSize
          let dataArr = that.practiseErrorList;
          console.log("===data====",data)
          if (param.page == 1) {
            dataArr = []
          }
          if (!data || data.length == 0) {
            that.setData({ practiseErrorList: null })
          } else {
            if (dataArr == null) { dataArr = [] }
            dataArr = dataArr.concat(data)
            that.setData({ practiseErrorList: dataArr })
          }
          wx.hideLoading()
        },
        fail: function (res) {
          console.log("fail")
          wx.hideLoading()
          getApp().globalData.loadFail()
        }
      })
    },
    /* 组件事件集合 */
    tolinkUrl: function (e) {
      console.warn("=======e=======", e);
      let linkUrl = e.currentTarget.dataset.url;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>